<?php
declare (strict_types = 1);

namespace app\mobile\controller;

use think\facade\Request;
use think\facade\Db;
use think\facade\Cache;
use \app\common\Time;
class Order extends \app\mobile\controller\Auth
{
  public function index()
  {

    $date = input('date');
    $from = input('from','my');
    

    $query[] = ['order_status','in','0,1,2,3,5'];
    $query2[] = ['order_status','in','0,1,2,3,5'];
    $query2[] = ['fee','>',0];
    
    if ($from == 'my') {
      $uids = getTeamUids($this->uid);
      if (!$uids) $uids = '-1';
      $query[] = ['uid','in',$uids];
      $query2[] = ['to_uid','=',$this->uid];
    }elseif ($from == 'team') {
      $uids = getTeamUids($this->uid);
      if (!$uids) $uids = '-1';
      // print_r($uids);exit();
      $query[] = ['uid','in',$uids];
      $query2[] = ['to_uid','in',$uids];
    }
    
    if ($date == 'today') {
      list($st,$et) = Time::today();
      $query[] = ['pay_time','>',$st];
      $query[] = ['pay_time','<',$et];
      $query2[] = ['pay_time','>',$st];
      $query2[] = ['pay_time','<',$et];
      // $query->whereTime('pay_time','today');
      // $query2->whereTime('pay_time','today');
    }elseif ($date == 'yestoday') {
      list($st,$et) = Time::yesterday();
      $query[] = ['pay_time','>',$st];
      $query[] = ['pay_time','<',$et];
      $query2[] = ['pay_time','>',$st];
      $query2[] = ['pay_time','<',$et];
      // $query->whereTime('pay_time','yesterday');
      // $query2->whereTime('pay_time','yesterday');
    }elseif ($date == 'month') {
      list($st,$et) = Time::month();
      $query[] = ['pay_time','>',$st];
      $query[] = ['pay_time','<',$et];
      $query2[] = ['pay_time','>',$st];
      $query2[] = ['pay_time','<',$et];
      // $query->whereTime('pay_time','month');
      // $query2->whereTime('pay_time','month');
    }elseif ($date == 'lastmonth') {
      list($st,$et) = Time::lastMonth();
      $query[] = ['pay_time','>',$st];
      $query[] = ['pay_time','<',$et];
      $query2[] = ['pay_time','>',$st];
      $query2[] = ['pay_time','<',$et];
      // $query->whereTime('pay_time','last month');
      // $query2->whereTime('pay_time','last month');
    }

    // print_r($query2);exit();
    $total_count = Db::name('reward')
                ->where($query2)
                // ->fetchsql(true)
                // ->where('uid',$this->uid)
                ->count('id');
    // print_r($total_count);exit();
    $total['count'] = $total_count;
    // return $total_count;

    $income = Db::name('reward')
          ->where($query2)
          
          // ->fetchsql(true)
          ->sum('fee');
    // print_r($income);exit();
    $total['income'] = $income;
    $settle = Db::name('reward')
          ->where($query2)
          ->sum('fee');
    $total['settle'] = $settle;
    $meituan_count = Db::name('reward')
                ->where($query2)
                ->where('source','meituan')
                // ->where('uid',$this->uid)
                ->count('id');
    $meituan['count'] = $meituan_count;

    $meituan_income = Db::name('reward')
              ->where($query2)
              // ->where('uid',$this->uid)
              ->where('source','meituan')
              ->sum('fee');
    $meituan['income'] = $meituan_income;
    $meituan_settle = Db::name('reward')
              ->where($query2)
              // ->where('uid',$this->uid)
              ->where('source','meituan')
              ->sum('fee');
    $meituan['settle'] = $meituan_settle;

    $ele_count = Db::name('reward')
                ->where($query2)
                // ->where('uid',$this->uid)
                ->where('source','ele')
                ->count('id');
    $ele['count'] = $ele_count;
    $ele_income = Db::name('reward')
              ->where($query2)
              // ->where('uid',$this->uid)
              ->where('source','淘宝')
              ->sum('fee');
    $ele['income'] = $ele_income;
    $ele_settle = Db::name('reward')
              ->where($query2)
              // ->where('uid',$this->uid)
              ->where('source','淘宝')
          ->sum('fee');
    $ele['settle'] = $ele_settle;

   


    $r = array();
    $r['total'] = $total;
    $r['meituan'] = $meituan;
    $r['ele'] = $ele;
    //可提现
    $r['free_bonus'] = getDeposit($this->uid);

    return $this->doSuccess('ok',$r);
    # code...
  }
  public function list()
  {
    $rows = Db::name('order')
                ->where('id','>',0)
                ->select()->toArray();
    foreach ($rows as &$k) {
      if ($k['uid']) {
        $user = Db::name('user')->field('id,nick,head')->where('id',$k['uid'])->find();
        $k['user'] = $user;
      }
      if ($k['type'] == 'ele') {
        $k['type_txt'] = '饿了么';
      }else{
        $k['type_txt'] = '美团';
      }
      if ($k['status'] == 3) {
        $k['statu_txt'] = '已结算';
      }
    }
    $r['rows'] = $rows;
    return $this->doSuccess('ok',$r);
  }
}